//
//  SEGServiceMessageViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/8.
//

#import "SEGServiceMessageViewModel.h"
#import "MessageDataBaseModel.h"

@implementation SEGServiceMessageViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self seg_createMenuListCommand];
        [self seg_createMsgV3Command];
    }
    return self;
}
- (void)seg_createMenuListCommand {
    @weakify(self);
    self.menuListCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(RACTuple *tuple) {
        RACTupleUnpack(NSString *serviceNbr, NSString *serviceNumberId) = tuple;
        @strongify(self);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [self.messManager requestServicenbrMenuListWithServiceNbr:serviceNbr serviceNumberId:serviceNumberId communityId:kAccountCommunityID finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    [subscriber sendNext:@""];
                    [subscriber sendCompleted];
                } else {
                    NSError *error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }];
}
- (void)seg_createMsgV3Command {
    @weakify(self);
    self.msgV3Command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *groupID) {
        @strongify(self);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [self.messManager requestMessageList:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                self.dataArray= [[MessageDataBaseModel sharedDataBaseModel] getMessagesDescendingWithGroupId:groupID];
                [subscriber sendNext:@(self.dataArray.count)];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
}

#pragma mark - Getter
- (MessageRequestModel *)messManager {
    if (!_messManager) _messManager = [[MessageRequestModel alloc] init];
    return _messManager;
}

@end
